<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-shopping-bag-1"></i> 门店管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <!-- 级联选择器 -->
                <el-cascader
                    class="mr10"
                    placeholder="请选择门店名称"
                    :options="options"
                    :props="{ checkStrictly: true }"
                    clearable></el-cascader>
                <el-input v-model="form.name" placeholder="请输入门店名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索门店</el-button>
                <el-button type="primary" icon="el-icon-plus" @click="handleAdd">添加门店</el-button>
                <el-button type="primary" icon="el-icon-plus" @click="handleSearch" class="mr10">导入门店</el-button>
                <el-button
                    type="primary"
                    icon="el-icon-download"
                    class="handle-del mr10"
                    @click="dowAllSelection"
                >批量下载</el-button>   
                <el-button
                    type="primary"
                    icon="el-icon-delete"
                    class="handle-del mr10"
                    @click="delAllSelection"
                >批量删除</el-button>   
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="name" label="门店名"></el-table-column>
                <el-table-column label="品牌名">
                    <template slot-scope="scope">￥{{scope.row.money}}</template>
                </el-table-column>
                <el-table-column label="区域">
                    <template slot-scope="scope">￥{{scope.row.money}}</template>
                </el-table-column>
                <el-table-column prop="address" label="地址"></el-table-column>
                <el-table-column label="品类">
                    <template slot-scope="scope">￥{{scope.row.money}}</template>
                </el-table-column>
                <el-table-column label="标签">
                    <template slot-scope="scope">￥{{scope.row.money}}</template>
                </el-table-column>
                <el-table-column label="省">
                    <template slot-scope="scope">￥{{scope.row.money}}</template>
                </el-table-column>
                <el-table-column label="市">
                    <template slot-scope="scope">￥{{scope.row.money}}</template>
                </el-table-column>
                <el-table-column label="区">
                    <template slot-scope="scope">￥{{scope.row.money}}</template>
                </el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="50%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="门店名" label-width="120px">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="所属品牌" label-width="120px">
                    <el-select v-model="query.address" placeholder="请选择" class="handle-select mr10" style="width:100%">
                        <el-option key="1" label="厨房调味" value="厨房调味"></el-option>
                        <el-option key="2" label="米田粮油" value="米田粮油"></el-option>
                    </el-select>
                </el-form-item>
                <!-- 上传图片 -->
                <el-form-item label="店铺logo" label-width="120px">
                <el-upload
                    class="avatar-uploader"
                    action="https://jsonplaceholder.typicode.com/posts/"
                    :show-file-list="false"
                    :on-success="handleAvatarSuccess"
                    :before-upload="beforeAvatarUpload">
                    <img v-if="imageUrl" :src="imageUrl" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
                </el-form-item>
                <!-- 上传图片 -->
                <el-form-item label="营业执照" label-width="120px">
                <el-upload
                    class="avatar-uploader"
                    action="https://jsonplaceholder.typicode.com/posts/"
                    :show-file-list="false"
                    :on-success="handleAvatarSuccess"
                    :before-upload="beforeAvatarUpload">
                    <img v-if="imageUrl" :src="imageUrl" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
                </el-form-item>
                <el-form-item label="门店分类" label-width="120px">
                    <el-select v-model="query.address" placeholder="请选择" class="handle-select mr10" style="width:100%">
                        <el-option key="1" label="厨房调味" value="厨房调味"></el-option>
                        <el-option key="2" label="米田粮油" value="米田粮油"></el-option>
                    </el-select>
                </el-form-item>
                <!-- 地区三个下拉列表 -->
                <el-form-item label="地区" label-width="120px">
                    <el-select v-model="query.address" placeholder="请选择" class="handle-select mr10" style="width:30%">
                        <el-option key="1" label="厨房调味" value="厨房调味"></el-option>
                        <el-option key="2" label="米田粮油" value="米田粮油"></el-option>
                    </el-select>
                    <el-select v-model="query.address" placeholder="请选择" class="handle-select mr10" style="width:30%">
                        <el-option key="1" label="厨房调味" value="厨房调味"></el-option>
                        <el-option key="2" label="米田粮油" value="米田粮油"></el-option>
                    </el-select>
                    <el-select v-model="query.address" placeholder="请选择" class="handle-select mr10" style="width:30%">
                        <el-option key="1" label="厨房调味" value="厨房调味"></el-option>
                        <el-option key="2" label="米田粮油" value="米田粮油"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="门店地址" label-width="120px">
                    <el-input v-model="form.address" style="width:80%" class="mr10"  placeholder="请输入门店地址"></el-input> <el-button type="primary">查找</el-button>
                </el-form-item>
                <el-form-item label="门店经度" label-width="120px">
                    <el-input v-model="form.address" placeholder="请输入门店经度"></el-input>
                </el-form-item>
                <el-form-item label="门店纬度" label-width="120px">
                    <el-input v-model="form.money" placeholder="请输入门店纬度"></el-input>
                </el-form-item>
                <el-form-item label="美团外卖门店名" label-width="120px">
                    <el-input v-model="form.money" placeholder="请输入美团外卖门店名"></el-input>
                </el-form-item>
                <el-form-item label="美团点评门店名" label-width="120px">
                    <el-input v-model="form.money" placeholder="请输入美团点评门店名"></el-input>
                </el-form-item>
                <el-form-item label="饿了么门店名" label-width="120px">
                    <el-input v-model="form.money" placeholder="请输入饿了么门店名"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 添加弹出框 -->
        <el-dialog title="添加" :visible.sync="addVisible" width="50%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="门店名" label-width="120px">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="所属品牌" label-width="120px">
                    <el-select v-model="query.address" placeholder="请选择" class="handle-select mr10" style="width:100%">
                        <el-option key="1" label="厨房调味" value="厨房调味"></el-option>
                        <el-option key="2" label="米田粮油" value="米田粮油"></el-option>
                    </el-select>
                </el-form-item>
                <!-- 上传图片 -->
                <el-form-item label="店铺logo" label-width="120px">
                <el-upload
                    class="avatar-uploader"
                    action="https://jsonplaceholder.typicode.com/posts/"
                    :show-file-list="false"
                    :on-success="handleAvatarSuccess"
                    :before-upload="beforeAvatarUpload">
                    <img v-if="imageUrl" :src="imageUrl" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
                </el-form-item>
                <!-- 上传图片 -->
                <el-form-item label="营业执照" label-width="120px">
                <el-upload
                    class="avatar-uploader"
                    action="https://jsonplaceholder.typicode.com/posts/"
                    :show-file-list="false"
                    :on-success="handleAvatarSuccess"
                    :before-upload="beforeAvatarUpload">
                    <img v-if="imageUrl" :src="imageUrl" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
                </el-form-item>
                <el-form-item label="门店分类" label-width="120px">
                    <el-select v-model="query.address" placeholder="请选择" class="handle-select mr10" style="width:100%">
                        <el-option key="1" label="厨房调味" value="厨房调味"></el-option>
                        <el-option key="2" label="米田粮油" value="米田粮油"></el-option>
                    </el-select>
                </el-form-item>
                <!-- 地区三个下拉列表 -->
                <el-form-item label="地区" label-width="120px">
                    <el-select v-model="query.address" placeholder="请选择" class="handle-select mr10" style="width:30%">
                        <el-option key="1" label="厨房调味" value="厨房调味"></el-option>
                        <el-option key="2" label="米田粮油" value="米田粮油"></el-option>
                    </el-select>
                    <el-select v-model="query.address" placeholder="请选择" class="handle-select mr10" style="width:30%">
                        <el-option key="1" label="厨房调味" value="厨房调味"></el-option>
                        <el-option key="2" label="米田粮油" value="米田粮油"></el-option>
                    </el-select>
                    <el-select v-model="query.address" placeholder="请选择" class="handle-select mr10" style="width:30%">
                        <el-option key="1" label="厨房调味" value="厨房调味"></el-option>
                        <el-option key="2" label="米田粮油" value="米田粮油"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="门店地址" label-width="120px">
                    <el-input v-model="form.address" style="width:80%" class="mr10"  placeholder="请输入门店地址"></el-input> <el-button type="primary">查找</el-button>
                </el-form-item>
                <el-form-item label="门店经度" label-width="120px">
                    <el-input v-model="form.address" placeholder="请输入门店经度"></el-input>
                </el-form-item>
                <el-form-item label="门店纬度" label-width="120px">
                    <el-input v-model="form.money" placeholder="请输入门店纬度"></el-input>
                </el-form-item>
                <el-form-item label="美团外卖门店名" label-width="120px">
                    <el-input v-model="form.money" placeholder="请输入美团外卖门店名"></el-input>
                </el-form-item>
                <el-form-item label="美团点评门店名" label-width="120px">
                    <el-input v-model="form.money" placeholder="请输入美团点评门店名"></el-input>
                </el-form-item>
                <el-form-item label="饿了么门店名" label-width="120px">
                    <el-input v-model="form.money" placeholder="请输入饿了么门店名"></el-input>
                </el-form-item>
            </el-form>          
            <span slot="footer" class="dialog-footer">
                <el-button @click="addVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { fetchData } from '../../../api/index';
export default {
    name: 'basetable',
    data() {
        return {
            query: {
                address: '',
                name: '',
                pageIndex: 1,
                pageSize: 10
            },
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            addVisible: false,
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1,
            options: [{
            value: 'zhinan',
            label: '指南',
            children: [{
                value: 'shejiyuanze',
                label: '设计原则',
                children: [{
                value: 'yizhi',
                label: '一致'
                }, {
                value: 'fankui',
                label: '反馈'
                }, {
                value: 'xiaolv',
                label: '效率'
                }, {
                value: 'kekong',
                label: '可控'
                }]
            }, {
                value: 'daohang',
                label: '导航',
                children: [{
                value: 'cexiangdaohang',
                label: '侧向导航'
                }, {
                value: 'dingbudaohang',
                label: '顶部导航'
                }]
            }]
            }, {
            value: 'zujian',
            label: '组件',
            children: [{
                value: 'basic',
                label: 'Basic',
                children: [{
                value: 'layout',
                label: 'Layout 布局'
                }, {
                value: 'color',
                label: 'Color 色彩'
                }, {
                value: 'typography',
                label: 'Typography 字体'
                }, {
                value: 'icon',
                label: 'Icon 图标'
                }, {
                value: 'button',
                label: 'Button 按钮'
                }]
            }, {
                value: 'form',
                label: 'Form',
                children: [{
                value: 'radio',
                label: 'Radio 单选框'
                }, {
                value: 'checkbox',
                label: 'Checkbox 多选框'
                }, {
                value: 'input',
                label: 'Input 输入框'
                }, {
                value: 'input-number',
                label: 'InputNumber 计数器'
                }, {
                value: 'select',
                label: 'Select 选择器'
                }, {
                value: 'cascader',
                label: 'Cascader 级联选择器'
                }, {
                value: 'switch',
                label: 'Switch 开关'
                }, {
                value: 'slider',
                label: 'Slider 滑块'
                }, {
                value: 'time-picker',
                label: 'TimePicker 时间选择器'
                }, {
                value: 'date-picker',
                label: 'DatePicker 日期选择器'
                }, {
                value: 'datetime-picker',
                label: 'DateTimePicker 日期时间选择器'
                }, {
                value: 'upload',
                label: 'Upload 上传'
                }, {
                value: 'rate',
                label: 'Rate 评分'
                }, {
                value: 'form',
                label: 'Form 表单'
                }]
            }, {
                value: 'data',
                label: 'Data',
                children: [{
                value: 'table',
                label: 'Table 表格'
                }, {
                value: 'tag',
                label: 'Tag 标签'
                }, {
                value: 'progress',
                label: 'Progress 进度条'
                }, {
                value: 'tree',
                label: 'Tree 树形控件'
                }, {
                value: 'pagination',
                label: 'Pagination 分页'
                }, {
                value: 'badge',
                label: 'Badge 标记'
                }]
            }, {
                value: 'notice',
                label: 'Notice',
                children: [{
                value: 'alert',
                label: 'Alert 警告'
                }, {
                value: 'loading',
                label: 'Loading 加载'
                }, {
                value: 'message',
                label: 'Message 消息提示'
                }, {
                value: 'message-box',
                label: 'MessageBox 弹框'
                }, {
                value: 'notification',
                label: 'Notification 通知'
                }]
            }, {
                value: 'navigation',
                label: 'Navigation',
                children: [{
                value: 'menu',
                label: 'NavMenu 导航菜单'
                }, {
                value: 'tabs',
                label: 'Tabs 标签页'
                }, {
                value: 'breadcrumb',
                label: 'Breadcrumb 面包屑'
                }, {
                value: 'dropdown',
                label: 'Dropdown 下拉菜单'
                }, {
                value: 'steps',
                label: 'Steps 步骤条'
                }]
            }, {
                value: 'others',
                label: 'Others',
                children: [{
                value: 'dialog',
                label: 'Dialog 对话框'
                }, {
                value: 'tooltip',
                label: 'Tooltip 文字提示'
                }, {
                value: 'popover',
                label: 'Popover 弹出框'
                }, {
                value: 'card',
                label: 'Card 卡片'
                }, {
                value: 'carousel',
                label: 'Carousel 走马灯'
                }, {
                value: 'collapse',
                label: 'Collapse 折叠面板'
                }]
            }]
            }, {
            value: 'ziyuan',
            label: '资源',
            children: [{
                value: 'axure',
                label: 'Axure Components'
            }, {
                value: 'sketch',
                label: 'Sketch Templates'
            }, {
                value: 'jiaohu',
                label: '组件交互文档'
            }]
        }],
            value: ''

        };
    },
    created() {
        this.getData();
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        getData() {
            fetchData(this.query).then(res => {
                console.log(res);
                this.tableData = res.list;
                this.pageTotal = res.pageTotal || 50;
            });
        },
        //下拉列表触发不同事件
        handleCommand(command) {
        this.$message('click on item ' + command);
        },  
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageIndex', 1);
            this.getData();
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    this.$message.success('删除成功');
                    this.tableData.splice(index, 1);
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        //批量下载
        dowAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
                console.log(this.multipleSelection[i])
            }
            this.$message.success(`下载了${str}`);
            this.multipleSelection = [];
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form = row;
            this.editVisible = true;
        },
                //添加操作
        handleAdd(){
            this.addVisible = true;
        },
        // 保存编辑
        saveEdit() {
            this.editVisible = false;
            this.$message.success(`修改第 ${this.idx + 1} 行成功`);
            this.$set(this.tableData, this.idx, this.form);
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .table {
        width: 100%;
        font-size: 14px;
    }
    .red {
        color: #ff0000;
    }
    .mr10 {
        margin-right: 10px;
    }
    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 100px;
        height: 100px;
        display: block;
    }
    .el-upload--text {
        width: 180px;
    }
</style>
